#!/usr/bin/env python
# -*- coding: utf-8 -*-
# File  :report_table.py
# Time  :2025/4/24 16:46
# Email :fortunatedong@163.com
# Author:by Adonli
from datetime import datetime
import json
import os
from config import ROOT_DIR
today = datetime.today()
current_year = today.year
current_month = today.month

REPORT_TABLE_FILES_FILE = os.path.join(ROOT_DIR, f'Datas/report_table/{current_year}.json')
# CURRENT_REPORT_TABLE_FILE = os.path.join(ROOT_DIR, f'Datas/report_table/{current_year}/{current_year}-{current_month}.json')
# 加载用户数据
def load_report_table():
    try:
        with open(REPORT_TABLE_FILES_FILE, 'r', encoding='utf-8') as f:
            return json.load(f)
    except (FileNotFoundError, json.JSONDecodeError):
        return init_report_table()


# 保存用户数据
def save_report_table(default_users_role_permission):
    os.makedirs(os.path.dirname(REPORT_TABLE_FILES_FILE), exist_ok=True)
    with open(REPORT_TABLE_FILES_FILE, 'w', encoding='utf-8') as f:
        json.dump(default_users_role_permission, f, ensure_ascii=False, indent=4)
def init_report_table():
    if not os.path.exists(REPORT_TABLE_FILES_FILE):
        default_users_role_permission = [
            {
                "DATE": "2025-03",
                "AUTHOR": "张三",
                "DEPARTMENT": "项目开发部",
                "REPORTTYPE": "月报",
                "REPORTDETAIL":[
                    {"department": "项目开发部", "type": "自动化","detail":"爬虫","finsh":"是","effects":"好","work_hour":0.6,"workload":"60%","create_date":"2025-03-03"},
                ],
            },
            {
                "DATE": "2025-04",
                "AUTHOR": "李四",
                "DEPARTMENT": "项目开发部",
                "REPORTTYPE": "月报",
                "REPORTDETAIL": [
                    {"department": "研发部门", "type": "自动化", "detail": "爬虫", "finsh": "是", "effects": "好",
                     "work_hour": 0.6,"workload":"60%", "create_date": "2025-03-04"},
                ],
            },
        ]
        save_report_table(default_users_role_permission)
    return load_report_table()